import { Injectable } from '@angular/core';
import { Loading ,LoadingController } from 'ionic-angular';

/*** 加载中
* */
@Injectable()
export class LoadingService {

    private loader : Loading;
    constructor(private loadingCtrl: LoadingController) { }

    /*** 创建loading
    * */
    create(message, duration = 1000, cssClass= 'office-default-loading') {
        this.closeLoading();

        this.loader = this.loadingCtrl.create({
          content: message,
          duration: duration,
          cssClass: cssClass,
          dismissOnPageChange: true
        });
        this.loader.present();
    }

    /*** 创建不自动关闭loading
    * */
    createLoading(message, duration = 0, cssClass = 'office-default-loading') {
      this.create(message, duration , cssClass);
    }

    /*** 创建纯提示loading
    * */
    createOnlyContent(message, duration= 1000) {
      this.closeLoading();

      this.loader = this.loadingCtrl.create({
        content: message,
        spinner: 'hide',
        duration: duration,
        showBackdrop: true
      });
      this.loader.present();
    }

    /*** 关闭loading
    * */
    closeLoading(): Promise<any>
    {
      if (this.loader)
        return this.loader.dismiss();
    }
}
